"use strict";
var baseUrl = 'http://192.168.27.221:8095'
// var baseUrl = 'https://appjzxtest.jiaxunsoft.com'
//封包
function getPackage(data) {
    let data_str = '';
    if (data) {
        data_str = JSON.stringify(data);
    }
    let pack = {
			  language:1,
        isEncrypt: 'N',
        data: data_str
    }
    return pack;
}

function request(method, reqUrl, pack, fnSuccess) {
	pack = getPackage(pack);
	uni.request({
		method: method || 'POST',
		url: baseUrl + reqUrl,
		data: pack,
		header: {
			'Content-Type': 'application/x-www-form-urlencoded',
			'loginToken':getCache("token")
		},
		success: function(result) {
			let pack = result;

			 if (fnSuccess) {
			                if (fnSuccess.length == 1) {
			                    fnSuccess.call(this, pack);
			                } else if (fnSuccess.length == 2) {
			                    fnSuccess.call(this, pack, pack.data.data);
			                } else {
			                    fnSuccess.call();
			                }
			
			
			            } 
			            else {
			                showToast("操作成功");
			            }
		},
		fail: function(result) {
			try {
				if (result.errMsg.indexOf('request:fail') != -1) {
					alert("似乎已断开与互联网的连接");
				} else {
					alert("操作失败！");
				}
			} catch (e) {
				alert("操作失败！");
			}
		}
	})
}


function navigateTo(url) {
	uni.navigateTo({
		url: url
	});
}

function switchTab(url) {
	uni.switchTab({
		url: url
	});
}
function reLaunch(url) {
    uni.reLaunch({
        url: url
    });
}

function redirectTo(url) {
    uni.redirectTo({
        url: url
    });
}

function showToast(title, icon) {
	uni.showToast({
		title: title || '提示',
		icon: icon || 'success',
		duration: 2000
	});
}
// alert
function alert(msg, title, success) {
    uni.showModal({
        title: '温馨提示' || title,
        content: msg,
        showCancel: false,
        confirmColor: "#000",
        success: success || {}
    })
}
//设置缓存
function setCache(key, value) {
	if (key != null && value != null) {
		try {
			uni.setStorageSync(key, value);
			console.log('设置缓存' + key + '=' + value);
		} catch (e) {
			console.log(e, "保存缓存失败")
		}
	}
}
//取出缓存
function getCache(key) {
	var obj = null;
	if (key) {
		try {
			obj = uni.getStorageSync(key);
			console.log("取出缓存", obj)
		} catch (e) {
			console.log(e, "取出缓存失败")
		}
	}
	return obj;
}
//分享页面
function onShareAppMessage(title,url) {
	return {
		title: title || '',
		path: 'pages/home/home'||url
	}
}
// 手机验证
function isMobile(mobile) {
    // 最新正则
    var myreg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    return myreg.test(mobile);
}
// 身份证验证
function isId(isId) {
    var myreg = /\d{17}[\d|x]|\d{15}/;

    return myreg.test(isId);
}
// 邮箱验证
function iseMail(iseMail) {
    var myreg = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;

    return myreg.test(iseMail);
}
// QQ验证
function isQQ(isQQ) {
    var myreg = /^[1-9]\d{4,9}$/;

    return myreg.test(isQQ);
}
module.exports = {
	request: request,
	navigateTo: navigateTo,
	switchTab: switchTab,
	redirectTo:redirectTo,
	reLaunch:reLaunch,
	showToast: showToast,
	setCache: setCache,
	getCache: getCache,
	onShareAppMessage:onShareAppMessage,
	baseUrl:baseUrl,
	isMobile:isMobile,
	isId:isId,
	iseMail:iseMail,
	isQQ:isQQ,
}
